package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;


    /**
     * 查询取消预约次数
     * LocalDateTime stat = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
     * LocalDateTime end = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
     * 设置时分秒
     * 时间戳：Instant类型
     */
    @Override
    public Integer select() {
        //1.
        Integer userId = Integer.parseInt(UserThreadLocal.getSubject());
        LocalDateTime start = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime end = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        //2.
        Integer count = reservationMapper.select(userId,start,end);


        return count;
    }

    /**
     * 查询每个时间段剩余预约次数
     * localDateTime.plusMonths(30) 加上30分钟
     */
    @Override
    public List<ReservationVo> countByTime(Long time) {
        //1.
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time).plusHours(8);
        //2.
        List<ReservationVo> list = new ArrayList<>();
        //3.
        for (int i = 0; i < 21; i++) {
            Integer count = reservationMapper.countByTime(localDateTime);
            ReservationVo vo = new ReservationVo();
            vo.setCount(count);
            vo.setTime(localDateTime);
            list.add(vo);
            localDateTime = localDateTime.plusMonths(30);
        }

        return list;
    }

    /**
     * 新增预约
     * @param reservationDto
     */
    @Override
    public void insert(ReservationDto reservationDto) {
        //1.
        Reservation bean = BeanUtil.toBean(reservationDto, Reservation.class);
        //2.
        bean.setCreateTime(LocalDateTime.now());
        bean.setUpdateTime(LocalDateTime.now());
        bean.setStatus(0);
        bean.setCreateBy(Long.parseLong(UserThreadLocal.getSubject()));

        //3.
        reservationMapper.insert(bean);
    }

    /**
     * 分页查询预约
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<ReservationVo> searchByPage(Integer pageNum, Integer pageSize, Integer status) {
        //1.
        PageHelper.startPage(pageNum,pageSize);
        //2.
        Page<ReservationVo> page= reservationMapper.searchByPage(status);
        //3.
        return PageResponse.of(page, ReservationVo.class);
    }

    /**
     * 取消预约
     * @param id
     */
    @Override
    public void cancel(Long id) {
        long updateBy = Long.parseLong(UserThreadLocal.getSubject());

        reservationMapper.cancel(id,updateBy);
    }

    /**
    * 自动执行预约过期
    */
    @Override
    public void autoStatus() {
        LocalDateTime now = LocalDateTime.now();
        reservationMapper.autoStatus(now);
    }
}
